package servlet.rool;

import net.sf.json.JSONObject;
import pojo.AdRoss;
import pojo.Admin;
import service.AdRossService;
import service.AdminService;
import service.impl.AdRossServiceImpl;
import service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

/**
 * 用户修改的servlet
 */
@WebServlet(value = "/AdminUpdateServlet")
public class AdminUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        AdminService as=new AdminServiceImpl();
        AdRossService ars=new AdRossServiceImpl();
        Admin admin=new Admin();
        admin.setAdId(Integer.parseInt(request.getParameter("ID")));
        admin.setAdImg(request.getParameter("img"));
        admin.setAdAccount(request.getParameter("account"));
        admin.setAdName(request.getParameter("name"));
        admin.setAdSex(Integer.parseInt(request.getParameter("sex")));
        admin.setAdPhone(request.getParameter("phone"));
        admin.setAdEmail(request.getParameter("email"));
        admin.setAdTime(Timestamp.valueOf(request.getParameter("time")));
        admin.setAdDesc(request.getParameter("desc"));
        int bo=as.modifAdmin(admin);
        int bo2=0;
        AdRoss adRoss=ars.queryUrass(Integer.parseInt(request.getParameter("ID")));
        if(adRoss.getAdRoId()==0){
            adRoss.setAdminId(Integer.parseInt(request.getParameter("ID")));
            adRoss.setRoleId(Integer.parseInt(request.getParameter("role")));
            bo2=ars.inseUrass(adRoss);
        }else {
            bo2=ars.updateUrass(Integer.parseInt(request.getParameter("ID")),Integer.parseInt(request.getParameter("role")));
        }
        JSONObject json=new JSONObject();

        if(bo>0 && bo2>0){
            json.put("message","修改成功");
        }else{
            json.put("message","修改失败");
        }

        PrintWriter out = response.getWriter();
        out.print(json);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
